package com.example.demo.demos.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Component
public class WangWuHandler extends AbstractHandler {

    @Override
    public String backPay(String name) {

        User user = new User();
        user.setName("11");
        user.setAge(11);
        try {
            Aggregate ag = new ConcreteAggregate();
            ag.add(user);

            Iterator iterator = ag.getIterator();
            while (iterator.hasNext()) {
                Object next = iterator.next();
//                String str = next.toString();
                if (Objects.isNull(next)) {
                    System.out.println(next + "必填字段不能为空");
                    throw new NullPointerException();
                }
            }
        } catch (NullPointerException e) {
            throw new RuntimeException(e);
        }

        Collection<String> coll = new ArrayList<String>();
//        Collection<Integer> coll1 = new ArrayList<Integer>();
//        coll1.add(user.getAge());


        coll.add(user.getName());
//        coll.add(user.getAge());
        java.util.Iterator<String> iterator1 = coll.iterator();
        while (iterator1.hasNext()) {
            String str = iterator1.next();
            if (StringUtils.isBlank(str)) {
                System.out.println(str + "必填字段不能为空");
                throw new RuntimeException();
            }
        }

        try {
            return name + "支付成功";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Factory.register("银行卡", this);
    }
}

//抽象聚合
interface Aggregate {

    public void add(Object obj);

    public void remove(Object obj);

    public Iterator getIterator();
}

//具体聚合
class ConcreteAggregate implements Aggregate {

    private List<Object> list = new ArrayList<>();

    @Override
    public void add(Object obj) {
        list.add(obj);
    }

    @Override
    public void remove(Object obj) {
        list.remove(obj);
    }

    @Override
    public Iterator getIterator() {
        return new ConcreteIterator(list);
    }
}

//抽象迭代器
interface Iterator {
    Object first();

    Object next();

    boolean hasNext();
}

//具体迭代器
class ConcreteIterator implements Iterator {

    private List<Object> list = null;

    private int index = -1;

    public ConcreteIterator(List<Object> list) {
        this.list = list;
    }

    @Override
    public Object first() {
        index = 0;
        Object obj = list.get(index);
        return obj;
    }

    @Override
    public Object next() {
        Object obj = null;
        if (this.hasNext()) {
            obj = list.get(++index);
        }
        return obj;
    }

    @Override
    public boolean hasNext() {

        if (index < list.size() - 1) {
            return true;
        }
        return false;
    }
}
